if (typeof Object.assign != 'function') {
  Object.assign = function(target) {
    'use strict';
    if (target == null) {
      throw new TypeError('Cannot convert undefined or null to object');
    }

    target = Object(target);
    for (var index = 1; index < arguments.length; index++) {
      var source = arguments[index];
      if (source != null) {
        for (var key in source) {
          if (Object.prototype.hasOwnProperty.call(source, key)) {
            target[key] = source[key];
          }
        }
      }
    }
    return target;
  };
}

function herdI18nHelpersInit(h) {
  h.registerHelper('i18nEnv', function(options) {
    if (typeof window !== 'undefined') {
      return '';
    }
    var i18n = options.data.root['__I18N__'];
    var i18nEnv = {
      lng: i18n.lng,
      ns: i18n.ns,
      defaultNS: i18n.defaultNS,
    };
    return new h.SafeString('window.i18nEnv = ' + JSON.stringify(i18nEnv) + ';');
  });

  h.registerHelper('i18n', function(key) {
    var options = arguments[arguments.length -1];
    var i18n = (typeof window === 'undefined') ? options.data.root['__I18N__'] : window.i18n;
    if (i18n == null) {
      return key;
    }
    var context = Object.assign({}, options.hash);
    if (arguments.length > 2) {
      for (var i = 1; i < arguments.length - 1; i++) {
        context['' + (i - 1)] = arguments[i];
      }
    }
    if (options.hash.bundle) {
      return i18n.t(options.hash.bundle + ':@:' + key, context);
    }
    return i18n.t(key, context);
  });
};

if (typeof module === 'object') {
  module.exports = herdI18nHelpersInit;
} else {
  if (typeof Handlebars !== 'undefined') {
    herdI18nHelpersInit(Handlebars);
  }
}
